<?php
//Zend_Loader::loadFile('../application/admin/models/Template.php');
class Rates 
{
    /**
     * @var object Zend DB Adapter
     */
    private $_db;
    /**
     * @var string
     */
   
    public function __construct()
    {
			$this->_db = Zend_Registry::get('db');
    }
    
    public function listRates($hotel_id)
    {
    	$sql="select * from hotel_rates where hotel_id=".$hotel_id."	";
    	//	echo $sql;exit;
    	$getNewsletters = $this->_db->fetchAll($sql);
		
		return $getNewsletters;
	}
	
	public function getrate($rate_id)
    {
    	$sql="select * from hotel_rates where rate_id=".$rate_id."	";
    	//echo $sql;exit;
    	$getRates = $this->_db->fetchrow($sql);
		
		return $getRates;
	}
	

    public function getHotel($hotel_id)
    {
    	$sql="
			select hotel_id,hotel_title,hotel_city,hotel_country,hotel_description,
    	 hotel_contactperson,hotel_phone,hotel_fax,hotel_skype,is_Enabled
			from  Rates where   hotel_id='".$hotel_id."'
			
		";  
    	$getHotel = $this->_db->fetchrow($sql);	
		return $getHotel;       
    }

    public function addRate($form_values)
    {
    	$hotel_id = $form_values['hotel_id'];
    	$rate_category = $form_values['rate_category'];
    	$rate_basis = $form_values['rate_basis'];
    	$fromdate = $form_values['fromdate'];
    	$todate = $form_values['todate'];
    	$rate_additionalinfo = $form_values['rate_additionalinfo'];
    	
    	$sql="INSERT INTO hotel_rates(hotel_id, rate_category, rate_basis, fromdate, todate, rate_additionalinfo)
    		  VALUES(" . $hotel_id . ", '" . $rate_category . "', '" . $rate_basis . "', '" . $fromdate . "', '" . $todate . "', '".$rate_additionalinfo."')";
    	 //echo $sql;exit;
    	$addrate=$this->_db->exec($sql);
        if($addrate)
        {
        	echo "A new record has been succsfully created";
        }
    } 
    
    public function updateRate($form_values)
    {
    	$hotel_id = $form_values['hotel_id'];
        $rate_id=$form_values['rate_id'];
    	$rate_category = $form_values['rate_category'];
    	$rate_basis = $form_values['rate_basis'];
    	$fromdate = $form_values['fromdate'];
    	$todate = $form_values['todate'];
    	$rate_additionalinfo = $form_values['rate_additionalinfo'];
    	
    	$sql = "UPDATE hotel_rates 
    			SET rate_category='".$rate_category."', rate_basis='".$rate_basis."',
    				fromdate='".$fromdate."', todate='".$todate."',
    				rate_additionalinfo='".$rate_additionalinfo."' 
    			WHERE rate_id=".$rate_id;
    	// echo $sql;exit;
        $updaterates = $this->_db->exec($sql);        
        if($updaterates)
        {
        	echo "Record has been successfully updated";
        }
    }   

    public function deleteRate($rate_id)
    {
    	$sql = "DELETE FROM hotel_rates 
    			WHERE rate_id = " . $rate_id;
    	
    	
        $delrates = $this->_db->exec($sql);
      	if($delrates)
      	{
      		echo "Selected rate has been successfully deleted";
      	}
    	//echo "Selected rate has been successfully deleted";
    }

    public function updateratefeatures($request)
    {
            $dberror=0;
            //  print_r($request);
             $insertstructarray=array();
             foreach($request["ratekey"] as $key=>$val)
             {

                      $insertstructarray[$val]["ratekeyvalFB"]=$request["ratekeyvalFB"][$key];
                       $insertstructarray[$val]["ratekeyvalHB"]=$request["ratekeyvalHB"][$key];

             }
               print_r($insertstructarray);
                $this->_db->exec("SET AUTOCOMMIT=0");
                $this->_db->exec("START TRANSACTION");
                        $deletesql="delete from  ratestructuredata  where rateid =".$request["rate_id"]."";
		        $sqldelete =  $this->_db->exec($deletesql);
                        //echo $deletesql;
                foreach($insertstructarray as $structurekey=>$structurevalue){
                    if(strlen($structurekey)>0){
                        $sql1="insert  into  ratestructuredata (ratekey,ratevalFB,ratevalHB,rateid) values ('".$structurekey."','".$structurevalue["ratekeyvalFB"]."','".$structurevalue["ratekeyvalHB"]."',".$request["rate_id"].")";
                        //echo 1;
                        $sqlinsert=  $this->_db->exec($sql1);
                        if(mysql_error())
                        {
                            $dberror=1;
                        }
                      
                    }
                }
                if($dberror==0)
                {
                    $this->_db->exec("COMMIT");
                     echo "Product details has been successfully updated";
                }
                else
                {
                    $this->_db->exec("ROLLBACK");
                    echo "Update failed.Database error occured!";
                }


               // exit;
		//return $success;
    }

    public function getratefeatures($rate_id)
    {

        $sql="select * from ratestructuredata where rateid=".$rate_id."	";
    	 //echo $sql;exit;
    	$getratefeatures = $this->_db->fetchAll($sql);
        return $getratefeatures;
        
    }
    public function deleteratefeatures($arr)
    {
        $sql="delete  from ratestructuredata where id=".$arr["id"]." "	;
    	 //echo $sql;exit;
    	$deleteratefeatures = $this->_db->exec($sql);
        if($deleteratefeatures)
        {
            echo "Successfully deleted!";
        }
    }
}